<?php
namespace app\admin\controller;

use app\admin\model\Login as LoginModel;
use think\Controller;
use think\Loader;

class Login extends Controller
{
    public function login()
    {
    	if (request()->isPost()) {
    		# code...
    		$username = input('username');
    		$password = input('password');

    		$data = [
    			'username'  => $username,
    			'password'   => $password,
			];

			$validate = Loader::validate('Admin');
			if(!$validate->check($data)){
			    $error_msg = $validate->getError();
			    $this->error($error_msg);
			}
			//数据库验证
			$res = LoginModel::checkLogin($data);
			if (!$res) {
				# code...
				$this->error('用户名或密码错误');
			} else {
				# code...
				LoginModel::setCookie($res);
				$this->success('登录成功','index/index');
			}
			
    	} else {
    		# code...
    	}

        return $this->fetch();
    }

    public function logout()
    {
    	LoginModel::logout();
    	$this->redirect('login');
    }
    
}
